﻿using System;
using System.ComponentModel.Composition;
using Fisharoo.Common;
using Fisharoo.DataAccess.Interfaces;
using Fisharoo.Interfaces;
using Fisharoo.Web.Account.Interfaces;


namespace Fisharoo.Web.Account.Presenters
{
    public class EditAccountPresenter
    {
        private Fisharoo.DataAccess.Account account;
        private IEditAccount _view;
        [Import]
        private IUserSession _userSession;
        [Import]
        private IAccountService _accountService;
        [Import]
        private IAccountRepository _accountRepository;
        [Import]
        private IRedirector _redirector;
        [Import]
        private IEmail _email;

        public EditAccountPresenter()
        {
            MEFManager.Compose(this);
        }

        public void Init(IEditAccount View, bool IsPostBack)
        {
            _view = View;

            if (_userSession.CurrentUser != null)
                account = _userSession.CurrentUser;
            else 
                _redirector.GoToAccountLoginPage();

            if(!IsPostBack)
                LoadCurrentUser();
        }

        private void LoadCurrentUser()
        {
            _view.LoadCurrentInformation(_userSession.CurrentUser);
        }

        public void UpdateAccount(string OldPassword, string NewPassword, string Username,
            string FirstName, string LastName, string Email, 
            string ZipCode, DateTime BirthDate)
        {
            //verify that this user is the same as the logged in user
            if (OldPassword.Encrypt(OldPassword) == account.Password)
            {
                if (Email != _userSession.CurrentUser.Email)
                {
                    if (!_accountService.EmailInUse(Email))
                    {
                        account.Email = Email;
                        //TODO: for development disable sending emails, so setting the following to true
                        //in production this will be set to false
                        account.EmailVerified = true;
                        _email.SendEmailAddressVerificationEmail(account.Username, Email);                        
                    }
                    else
                    {
                        _view.ShowMessage(false, "The email your entered is already in our system!", account.Username);
                        return;
                    }
                }

                if (!string.IsNullOrEmpty(NewPassword))
                    account.Password = NewPassword.Encrypt(NewPassword);

                account.FirstName = FirstName;
                account.LastName = LastName;

                account.Zip = ZipCode;
                account.BirthDate = BirthDate;

                _accountRepository.SaveAccount(account);
                _view.ShowMessage(true, "Your account has been updated!", account.Username);
            }
            else
            {
                _view.ShowMessage(false, "The password you entered doesn't match your current password!  Please try again.", account.Username);
            }
        }
    }
}
